#include <stdio.h>
#include <unistd.h>
#include <iot_gpio.h>
#include <iot_gpio_ex.h>
#include <iot_spi.h>
#include "oled.h"
#include <base_type.h>

#include "dbg.h"
#include "ohos_init.h"
#include "cmsis_os2.h"

#define OLED_TASK_STACK_SIZE 4096

// width: 71, height: 44
const unsigned char col[] = { 0xff,0xff,0xff,0xff,0xff,0x80,0x3f,0xff,0xfe,0xff,0xff,0xff,0xff,0x87,0x0e,0x0f,0xff,0xfe,0xff,0xff,0xff,0xfc,0x00,0x3f,0xc3,0xff,0xfe,0xff,0xff,0xff,0xf0,0x70,0x7f,0xf0,0xff,0xfe,0xff,0xff,0xff,0xc1,0xff,0xff,0xf8,0x7f,0xfe,0xff,0xff,0xff,0x0f,0xff,0xff,0xfe,0x3f,0xfe,0xff,0xff,0xfe,0x1f,0x7e,0x63,0xff,0x1f,0xfe,0xff,0xff,0xf8,0x7f,0xfe,0x40,0xff,0x8f,0xfe,0xff,0xff,0xf0,0xff,0xfc,0x00,0x7f,0xc7,0xfe,0xff,0xff,0xe3,0xff,0xfc,0x00,0x3f,0xe3,0xfe,0xff,0xff,0xc7,0xff,0xf8,0x00,0x1d,0xf1,0xfe,0xff,0xff,0x8f,0xff,0xf8,0x3f,0x8c,0xf8,0xfe,0xff,0xff,0x1f,0xfb,0xf9,0xff,0x8c,0x7c,0xfe,0xff,0xfe,0x3f,0xff,0xf9,0xff,0xc6,0x3c,0x7e,0xff,0xfc,0x7e,0x7f,0xf8,0x3f,0xc7,0x3e,0x7e,0xff,0xfc,0xfc,0x7f,0xf8,0x0f,0xc7,0x1e,0x3e,0xff,0xf8,0xfc,0xff,0xf9,0xc7,0x83,0x9f,0x3e,0xff,0xf9,0xfc,0xff,0xf1,0xf3,0x03,0x8f,0x3e,0xff,0xf0,0x18,0xff,0xf1,0xf9,0x30,0xcf,0x3e,0xff,0xf2,0x58,0xff,0xf1,0xf8,0x70,0x07,0x9e,0xff,0xf2,0x58,0xff,0x91,0xc4,0x7c,0x07,0x9e,0xff,0xf0,0x58,0xfb,0x81,0x84,0xfc,0x27,0x9e,0xff,0xfc,0x58,0xf9,0x81,0x84,0xfc,0x23,0x1e,0xff,0xfe,0x58,0xf0,0x01,0xc4,0xfc,0x41,0x3e,0xff,0xfe,0x58,0xf0,0x1f,0xf8,0x7f,0xc0,0x3e,0xff,0xfe,0x58,0xf0,0x8f,0xf8,0x7f,0x98,0x7e,0xff,0xfe,0x40,0xf0,0xe7,0xf0,0x1f,0x30,0xfe,0xff,0xfe,0x00,0xe0,0xf0,0x00,0x00,0x40,0xfe,0xff,0xff,0x00,0x00,0x7c,0x0f,0xc1,0xc0,0xfe,0xff,0xff,0xcc,0x00,0x7f,0x9f,0xf3,0x80,0xfe,0xff,0xf0,0x0c,0x00,0x7f,0x3f,0xf3,0x80,0xfe,0xff,0xc0,0x04,0x00,0x7f,0x3f,0xe1,0x80,0xfe,0xff,0x00,0x04,0x00,0x7f,0x00,0x03,0x80,0xfe,0xfe,0x00,0x06,0x00,0xff,0x00,0x13,0x81,0xfe,0xfc,0x00,0x03,0x00,0xff,0x8f,0xe7,0x81,0xfe,0xf8,0x00,0x01,0x83,0xff,0xe0,0x0f,0xc3,0xfe,0xf0,0x00,0x00,0xff,0x81,0xf8,0x3f,0xe7,0xfe,0xf0,0x00,0x00,0x40,0x00,0x03,0xff,0x87,0xfe,0xe0,0x00,0x00,0x00,0x00,0x00,0x1f,0x01,0xfe,0xe0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xfe,0xe0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xfe,0xe0,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xfe,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xfe,0xc0,0x00,0x07,0xc0,0x00,0x00,0x00,0x0d,0xfe };


void OLedTask(void * para) /* task处理函数 */
{
 
    //test_led_screen();
    OLED_Init();
    OLED_Clear(0);
    while (1)
    {
        OLED_Clear(0);
        // GUI_DrawLine(1,1,10,10,1);
        // GUI_DrawCircle(24,24,1,8);
        GUI_ShowString(1,1,"2023095033",16,1);
        GUI_ShowChinese(80,1,16,"刘金南",1);
        GUI_DrawBMP(1,20,71,44,col,1);
        sleep(1);
    }
    

    return NULL;
}

void oled_demo(void)
{    
    int ret;
    oled_gpio_io_init();

    IotSpiCfgInitParam init_param={
        .isSlave = 0
    };

    // IotSpiCfgBasicInfo cfg_param={
    //      .cpol = IOT_SPI_CFG_CLOCK_CPOL_0,
    //      .cpha = IOT_SPI_CFG_CLOCK_CPHA_0,
    //      .dataWidth = IOT_SPI_CFG_DATA_WIDTH_E_8BIT,
    //      .endian = IOT_SPI_CFG_ENDIAN_LITTLE,
    //      .framMode = IOT_SPI_CFG_FRAM_MODE_MOTOROLA,
    //      .freq = 4000000
    // };

    // IoTSpiInit(0, init_param,&cfg_param);

    screen_spi_master_init(0); 
    osThreadAttr_t attr;
 
    attr.name = "OLedTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = OLED_TASK_STACK_SIZE;
    attr.priority = 20;
    if (osThreadNew((osThreadFunc_t)OLedTask, NULL, &attr) == NULL) {
        printf("[OLedExample] Falied to create LedTask1!\n");
    }
}
APP_FEATURE_INIT(oled_demo);
